﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Lucene.Net;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Search.Highlight;
using Lucene.Net.Store;
using Lucene.Net.Index;
using System.Drawing;
using PanGu;
using PanGu.HighLight;
using Lucene.Net.Analysis.PanGu;

public partial class register_4 : System.Web.UI.Page
{
    string enterid;
    bool check = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] == null || Session["PASSWORD"] == null)
            Response.Redirect("register_1.aspx", true);//防止利用URL直接访问
        else
        { 
            //添加企业的索引，利用注册信息创建第一条索引，创建前还要验证Index_enterid是否存在，如果该路径存在则说明已经有了。。如果没有则创建新的
            string name = Session["USERNAME"].ToString();
            SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc.Open();
            SqlCommand get_eid = new SqlCommand("select ENTER_ID from INFO_ENTER where ENTER_NAME=@name",sc);
            get_eid.Parameters.AddWithValue("@name",name);
            enterid = get_eid.ExecuteScalar().ToString();
            string fpath = "index/index_" + enterid;
            if (System.IO.Directory.Exists(Server.MapPath(fpath)) == false)//如果不存在就创建index_enterid文件夹 
            {
                System.IO.Directory.CreateDirectory(Server.MapPath(fpath));

                IndexWriter writer = new IndexWriter(Server.MapPath(fpath), new PanGuAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
                Document doc = new Document();
                doc.Add(new Lucene.Net.Documents.Field("pid", "P10000", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED));
                doc.Add(new Lucene.Net.Documents.Field("name", "注册信息", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED));
                doc.Add(new Lucene.Net.Documents.Field("text", name, Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED));
                writer.AddDocument(doc);
                writer.Optimize();
                writer.Close();
            }
            sc.Close();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (textbox1.Text == "" && textbox2.Text == "" && textbox3.Text == "" && textbox4.Text == "")
        {
            Page.RegisterStartupScript("ss", "<script>alert('您还没有输入您的主要产品分类，请至少输入一个分类！')</script>");
            return;
        }
        if (textbox1.Text.Length > 5 || textbox2.Text.Length > 5 || textbox3.Text.Length > 5 || textbox4.Text.Length > 5)
        {
            Page.RegisterStartupScript("ss", "<script>alert('请注意分类名称长度不要超过五个字符！')</script>");
            return;
        }
        if (textbox1.Text.Equals(textbox2.Text) == true || textbox1.Text.Equals(textbox3.Text) == true || textbox1.Text.Equals(textbox4.Text) == true || textbox2.Text.Equals(textbox3.Text) == true || textbox2.Text.Equals(textbox4.Text) == true || textbox3.Text.Equals(textbox4.Text) == true)
        {
            Page.RegisterStartupScript("ss", "<script>alert('请不要录入名称相同的产品分类！')</script>");
            return;
        }
        SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc1.Open();
        int count= 0;
        try
        {
            if (textbox1.Text != "")
            {//写入数据库

                SqlCommand insert_sort1 = new SqlCommand();
                insert_sort1.Connection = sc1;
                insert_sort1.CommandText = "insert into INFO_SORT values (@name,@eid,@sl)";
                insert_sort1.Parameters.AddWithValue("@name", textbox1.Text);
                insert_sort1.Parameters.AddWithValue("@eid", enterid);
                insert_sort1.Parameters.AddWithValue("@sl", "主要产品");
                while (Application["sqllock"] == "lock")
                {//检测是否已经锁住               
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁 
                try
                {
                    insert_sort1.ExecuteNonQuery();
                }
                finally
                {
                    Application["sqllock"] = "unlock";//解锁 
                }
                count++;

            }
            if (textbox2.Text != "")
            {
                SqlCommand insert_sort2 = new SqlCommand();
                insert_sort2.Connection = sc1;
                insert_sort2.CommandText = "insert into INFO_SORT values (@namea,@eida,@sla)";
                insert_sort2.Parameters.AddWithValue("@namea", textbox2.Text);
                insert_sort2.Parameters.AddWithValue("@eida", enterid);
                insert_sort2.Parameters.AddWithValue("@sla", "主要产品");
                while (Application["sqllock"] == "lock")
                {//检测是否已经锁住               
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁 
                try
                {
                    insert_sort2.ExecuteNonQuery();
                }
                finally
                {
                    Application["sqllock"] = "unlock";//解锁 
                }

                count++;

            }
            if (textbox3.Text != "")
            {

                SqlCommand insert_sort3 = new SqlCommand();
                insert_sort3.Connection = sc1;
                insert_sort3.CommandText = "insert into INFO_SORT values (@nameb,@eidb,@slb)";
                insert_sort3.Parameters.AddWithValue("@nameb", textbox3.Text);
                insert_sort3.Parameters.AddWithValue("@eidb", enterid);
                insert_sort3.Parameters.AddWithValue("@slb", "主要产品");
                while (Application["sqllock"] == "lock")
                {//检测是否已经锁住               
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁 
                try
                {
                    insert_sort3.ExecuteNonQuery();
                }
                finally
                {
                    Application["sqllock"] = "unlock";//解锁 
                }

                count++;

            }
            if (textbox4.Text != "")
            {

                SqlCommand insert_sort4 = new SqlCommand();
                insert_sort4.Connection = sc1;
                insert_sort4.CommandText = "insert into INFO_SORT values (@namec,@eidc,@slc)";
                insert_sort4.Parameters.AddWithValue("@namec", textbox1.Text);
                insert_sort4.Parameters.AddWithValue("@eidc", enterid);
                insert_sort4.Parameters.AddWithValue("@slc", "主要产品");
                while (Application["sqllock"] == "lock")
                {//检测是否已经锁住               
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁 
                try
                {
                    insert_sort4.ExecuteNonQuery();
                }
                finally
                {
                    Application["sqllock"] = "unlock";//解锁 
                }

                count++;

            }
        }
        catch (Exception ex_sql)
        {
            Application["sqllock"] = "unlock";//解锁 
            Page.RegisterStartupScript("ss", "<script>alert('数据库异常，请重试！')</script>");
            return;
        }
        if (count > 0)
            check = true;
        else check = false;
        if (check == true)
        {
            Response.Redirect("main.aspx");
        }
        else
        {
            Page.RegisterStartupScript("ss", "<script>alert('请至少录入一个分类信息后才能继续！')</script>");
            return;
        }
        
    }
}